using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Universe.WCF.Behaviors.WcfStreaming;

namespace WcfStreamTests
{
    public static class StreamExtensions 
    {
        public static long ReadToEnd(this HashReaderStream stream, out byte[] data, out string hash, int bytesPerRquest)
        {
            MemoryStream mem = new MemoryStream();
            byte[] buffer = new byte[bytesPerRquest];
            while(true)
            {
                int n = stream.Read(buffer, 0, buffer.Length);
                if (n <= 0)
                    break;

                mem.Write(buffer, 0, n);
            }

            stream.Close();
            hash = string.Join("", stream.Hash.Select(x => x.ToString("X2")).ToArray());
            data = mem.ToArray();
            return data.Length;
        }
    }
}